﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public class EditorMethodWindow : EditorWindow
{
    [MenuItem("DevelopTool/Create Editor Method")]
    static void CreateEditorMethod()
    {
        EditorWindow win = GetWindow(typeof(EditorMethodWindow), false);
    }

    EditorMethodWindow()
    {
        this.titleContent = new GUIContent("Create New Editor Method");
        this.maxSize = new Vector2(300, 100);
        this.minSize = new Vector2(300, 100);
    }

    bool isDisablePanel = false;
    string methodName;
    string savePath;
    bool isCheck = false;

    private void Update()
    {
        if (isCheck)
        {
            if (!EditorApplication.isCompiling)
            {
                isCheck = false;
                isDisablePanel = false;
                methodName = "";
                Debug.Log("----Success Create Editor Method!----");
            }
        }
    }

    private void OnGUI()
    {
        EditorGUI.BeginDisabledGroup(isDisablePanel);

        GUILayout.BeginVertical();
        GUILayout.Space(20);

        GUILayout.BeginHorizontal();
        GUILayout.Space(45);
        GUILayout.Label("Method Name:");
        methodName=GUILayout.TextField(methodName,GUILayout.Width(120));
        for (int i = 0; i < 7; i++)
        {
            GUILayout.Label("");
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(10);
        GUILayout.BeginHorizontal();
        GUILayout.Space(45);
        if (GUILayout.Button("Create", GUILayout.Width(210)))
        {
            if (!string.IsNullOrEmpty(methodName))
            {
                isDisablePanel = true;
                savePath = Application.dataPath + "/Editor/CustomEditor/";
                if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
                GenerateClassFile();
                AssetDatabase.Refresh();
            }
            else
            {
                Debug.LogError("Method Name not null!");
            }
        }
        GUILayout.EndHorizontal();
        
        GUILayout.EndVertical();

        EditorGUI.EndDisabledGroup();
    }

    void GenerateClassFile()
    {
        string filePath = savePath + methodName + ".cs";
        if (File.Exists(filePath)) File.Delete(filePath);
        using (StreamWriter file = new StreamWriter(filePath, false))
        {
            //类文件头部
            file.Write("using System;\n");
            file.Write("using System.Collections;\n");
            file.Write("using System.Collections.Generic;\n");
            file.Write("using UnityEngine;\n");
            file.Write("using UnityEditor;\n");
            file.Write("\n");
            //写入类名(与文件名相同)
            file.Write("public class ");
            file.Write(methodName);
            file.Write("\n{\n");

            file.Write("    [MenuItem(\"DevelopTool/Custom Method/" +methodName+ "\",false,11)]\n");
            file.Write("    static void "+methodName+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+ DateTime.Now.Minute.ToString()+"(){\n");
            file.Write("    \n");
            file.Write("    }\n");

            file.Write("\n}");
            isCheck = true;
        }
    }
}
